package com.dunqi.gpm.shunqing.develop.adapter;

import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.base.RecyclerAdapter;
import com.dunqi.gpm.shunqing.common.util.TimeUtil;
import com.dunqi.gpm.shunqing.develop.activity.URLSpanToWeb;
import com.dunqi.gpm.shunqing.develop.bean.ProjectNews;

import java.util.List;

/**
 * Created by Administrator on 2017/12/10.
 */
public class NewsAdapter extends RecyclerAdapter<ProjectNews> {
    public NewsAdapter(@Nullable List<ProjectNews> data) {
        super(R.layout.item_develop_news, data);
    }
    //            'county_leader_link', '区长外部链接
    //            'county_sec_link', '书记外部链接',

    @Override
    protected void convert(final BaseViewHolder helper, ProjectNews item) {
        final String content = item.getDetail();

        Byte objectType = item.getObjectType();
        String msgType = item.getMsgType();

        TextView contentTv = helper.getView(R.id.content_tv);

        helper.setText(R.id.title_tv, item.getTitle())
                .setText(R.id.time_tv, TimeUtil.getTen(item.getCreateTime()));

        TextView up = helper.getView(R.id.up);

        up.setText(item.getMsgTypeText());
//        if (objectType == 2) {
//            up.setVisibility(View.VISIBLE);
//        } else {
//            up.setVisibility(View.GONE);
//        }

        if (objectType == 3) {
            if ("county_leader_link".equals(msgType) || "county_sec_link".equals(msgType)) {
                contentTv.setText("");
            } else {
//                String baseUrl = UserUtil.getBaseUrl();
//                contentTv.setText(baseUrl + content);
                contentTv.setText("");
            }

        } else {
            contentTv.setText(content);
        }

        CharSequence text = contentTv.getText();

        if (text instanceof Spannable) {
            int end = text.length();
            Spannable sp = (Spannable) text;
            URLSpan urls[] = sp.getSpans(0, end, URLSpan.class);
            SpannableStringBuilder style = new SpannableStringBuilder(text);
            style.clearSpans();
            for (URLSpan urlSpan : urls) {
                URLSpanToWeb myURLSpan = new URLSpanToWeb(urlSpan.getURL());
                style.setSpan(myURLSpan, sp.getSpanStart(urlSpan),
                        sp.getSpanEnd(urlSpan),
                        Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

            }
            contentTv.setText(style);
        }
        CharSequence contentTvText = contentTv.getText();
        if (TextUtils.isEmpty(contentTvText)) {
            contentTv.setVisibility(View.GONE);
        } else {
            contentTv.setVisibility(View.VISIBLE);
        }

    }

}
